'use strict';

/**
 * Created by zhu on 14/12/21.
 */
angular.module('blog')
    .controller('BlogCategoryCtrl', [
        '$scope',
        '$rootScope',
        '$routeParams',
        '$window',
        'BlogCategoryQueryService',
        'PathSuffixConstant',
        'BlogCategoryModalService',
        'BlogRecycleQueryService',
        function ($scope, $rootScope, $routeParams, $window, BlogCategoryQueryService, PathSuffixConstant, BlogCategoryModalService, BlogRecycleQueryService) {
            $rootScope.visitedUserId = $routeParams.userId;

            /**
             * 加载目录
             */
            BlogCategoryQueryService.query({author: $scope.visitedUserId + PathSuffixConstant}, function (response) {
                $scope.categories = response;
            });

            /**
             * 加载回收站
             */
            BlogRecycleQueryService.query({author: $scope.visitedUserId + PathSuffixConstant}, function (response) {
                $scope.recycleBlogCount = response.totalCount;
            });

            $scope.categoryManagement = {
                show: BlogCategoryModalService.activate
            };

            var CategoryEvents = [
                'CategoryCreatedEventFromRoot',
                'CategoryRenamedEventFromRoot',
                'CategoryDeletedEventFromRoot'
            ];

            angular.forEach(CategoryEvents, function (v) {
                $scope.$on(v, function (event, data) {
                    $scope.categories = data;
                });
            });

            $scope.goToCollect = function () {
                $window.location.href = $rootScope.thirdPartyPaths.collect + $routeParams.userId;
            };

        }])

    .controller('BlogCategoryManagementCtrl', [
        '$scope',
        '$rootScope',
        '$routeParams',
        'BlogCategoryModalService',
        'BlogCategoryQueryService',
        'BlogCategoryModifyService',
        'PathSuffixConstant',
        function ($scope, $rootScope, $routeParams, BlogCategoryModalService, BlogCategoryQueryService, BlogCategoryModifyService, PathSuffixConstant) {
            this.close = BlogCategoryModalService.deactivate;
            var self = this;

            $scope.visitedUserId = $routeParams.userId;

            /**
             * 查询当前目录
             */
            $scope.query = function () {
                BlogCategoryQueryService.query({author: $scope.visitedUserId + PathSuffixConstant}, function (response) {
                    $scope.categories = response;
                });
            };

            $scope.query();

            /**
             * 删除目录
             *
             * @param categoryId
             */
            $scope.remove = function (category) {
                if (confirm("确认要删除目录：" + category.name + "吗？")) {
                    BlogCategoryModifyService.remove({id: category._id}, function () {
                        var newCategories = [];
                        angular.forEach($scope.categories, function (v) {
                            if (v._id != category._id) {
                                newCategories.push(v);
                            }
                        });
                        $scope.categories = newCategories;
                        $scope.$emit('CategoryDeletedEvent', $scope.categories);
                    });
                }
            };

            /**
             * 增加目录
             */
            $scope.add = function () {
                if (!$scope.newCategoryName || $scope.newCategoryName == '') {
                    alert('请输入目录名称');
                    return;
                }

                BlogCategoryModifyService.save({
                    author: {identity: $scope.visitedUserId + PathSuffixConstant},
                    categoryName: $scope.newCategoryName
                }, function (category) {
                    var newCategory = initializeCategory(category);
                    $scope.categories.push(newCategory);
                    $scope.$emit('CategoryCreatedEvent', $scope.categories);
                    $scope.newCategoryName = '';
                });

                function initializeCategory(category) {
                    category.size = 0;
                    category.name = $scope.newCategoryName;
                    return category;
                }
            };

            /**
             * 目录处于编辑模式
             *
             * @param category
             */
            $scope.edit = function (category) {
                category.oldName = category.name;
                category.editable = true;
            };

            /**
             * 保存目录修改
             *
             * @param category
             */
            $scope.edit_save = function (category) {
                var changeRequest = {
                    categoryId: category._id,
                    changedName: category.name
                };

                BlogCategoryModifyService.modify({id: category._id}, changeRequest, function () {
                    $scope.$emit('CategoryRenamedEvent', $scope.categories);
                    category.editable = false;
                });
            };

            /**
             * 放弃目录保存
             *
             * @param category
             */
            $scope.edit_cancel = function (category) {
                category.name = category.oldName;
                category.editable = false;
            };

            $rootScope.$on('CategoryCreatedEvent', function (event, data) {
                $rootScope.$broadcast('CategoryCreatedEventFromRoot', data);
            });

            $rootScope.$on('CategoryRenamedEvent', function (event, data) {
                $rootScope.$broadcast('CategoryRenamedEventFromRoot', data);
            });

            $rootScope.$on('CategoryDeletedEvent', function (event, data) {
                $rootScope.$broadcast('CategoryDeletedEventFromRoot', data)
            });

        }]);
